<?php

/*
Plugin Name: petfinder
Description: PHP class for communicating with Petfinder.com API
requires signing up for a free API key from Petfinder website
Version: 1.0
Author: Kyle Phillipeck
License: MIT
*/
	function import() {
		include_once 'import.php';
	}
	function update() {
		include_once 'update.php';
	}
	if (!wp_next_scheduled('updateAnimals_hook')) {
		wp_schedule_event( time(), 'daily', 'updateAnimals_hook' );
	}
	add_action( 'updateAnimals_hook', 'update' );

	class animals {
		static $add_script;

		static function init() {
			add_shortcode('animals', array(__CLASS__, 'handle_shortcode'));

			add_action('init', array(__CLASS__, 'register_script'));
			add_action('wp_footer', array(__CLASS__, 'print_script'));
		}

		static function handle_shortcode($atts) {
			self::$add_script = true;
			getsort();
		}

		static function register_script() {
			wp_register_script('custom', plugins_url('custom.js', __FILE__), array('jquery', 'jquery-form'), '1.0', true);
			//wp_register_script('jquery-form' , get_bloginfo('wpurl').' /wp-includes/js/jquery/jquery.form.js', array('jquery'));
		}

		static function print_script() {
			if ( ! self::$add_script )
				return;

			wp_print_scripts('custom');
		}
	}

	animals::init();

	function getSort() {

		?>
		<form name="ptfndr_search" action="" method="get">
			<select name="sO">
				<option value="ASC" <?php (isset($_GET['sO']) && $_GET['sO'] === "ASC") ? print 'selected=\"selected\"' : print '' ?>>Ascending</option>
				<option value="DESC" <?php (isset($_GET['sO']) && $_GET['sO'] === "DESC") ? print 'selected=\"selected\"' : print '' ?>>Descending</option>
			</select>
			<select name="sF">
				<option value="name" <?php (isset($_GET['sF']) && $_GET['sF'] === "name") ? print 'selected=\"selected\"' : print '' ?>>Name</option>
				<option value="lastUpdate" <?php (isset($_GET['sF']) && $_GET['sF'] === "lastUpdate") ? print 'selected=\"selected\"' : print '' ?>>Date Added</option>
				<option value="age" <?php (isset($_GET['sF']) && $_GET['sF'] === "age") ? print 'selected=\"selected\"' : print '' ?>>Age</option>
				<option value="sex" <?php (isset($_GET['sF']) && $_GET['sF'] === "sex") ? print 'selected=\"selected\"' : print '' ?>>Sex</option>
				<option value="breeds" <?php (isset($_GET['sF']) && $_GET['sF'] === "breeds") ? print 'selected=\"selected\"' : print '' ?>>Breed</option>
				<option value="size" <?php (isset($_GET['sF']) && $_GET['sF'] === "size") ? print 'selected=\"selected\"' : print '' ?>>Size</option>
			</select>
			<input type="submit" value="Search">
		</form>
    <div id="ptfndr_loading"></div>
		<div id="ptfndr_results"></div>
		<?php

		$defaultSortOrder = "ASC";
				$defaultSortField = "name";
				$whitelistOrder = array("DESC", "ASC");
				$whitelistField = array("name", "lastUpdate", "age", "sex", "breeds", "size");

				if((!isset($_GET["sO"]) && !isset($_GET["sF"])) ||
					(!in_array($_GET["sO"],$whitelistOrder) || !in_array($_GET["sF"], $whitelistField))){
					$sortOrder = $defaultSortOrder;
					$sortField = $defaultSortField;
				}else {
					$sortOrder = $_GET["sO"];
					$sortField = $_GET["sF"];
				}

				$sql = 'SELECT * FROM animals ORDER BY '.$sortField.' '.$sortOrder.'';

				include_once 'opendb.php';
				$result = $db->query($sql);
				while($row = $result->fetch_array(MYSQLI_ASSOC)) {
					$data[] = $row;
				}

				include('class.pagination.php');
				$objPg = new Pagination();
				$html = $objPg->getArrRows($data);
        print "<div id=\"ptfndrRes\">";
				print $objPg->getLinks();
				print $html;
				print $objPg->getLinks();
        print "</div>";

		include_once 'closedb.php';

	}

	//add_shortcode('animals', getSort);


	function ptfndr_orderByRecentDesc() {
		$sql = 'SELECT * FROM animals ORDER BY lastUpdate DESC';
		ptfndr_printFromDB($sql);

	}
	function ptfndr_printFromDB($sql) {

	}

	function petfinder_admin() {
		if (!current_user_can('manage_options'))  {
			wp_die( __('You do not have sufficient permissions to access this page.') );
		}
		include('petfinder_import_admin.php');
	}

	function petfinder_admin_options () {
		add_options_page('Petfinder Pet Display', 'Petfinder', 'manage_options', 'Petfinder-pet-display', 'petfinder_admin');
	}

	add_action('admin_menu', 'petfinder_admin_options');

?>